# include<stdio.h>
# include<stdlib.h>

int main(int argc , char* argv[])
{
	//大前提：如果参数数量不等于3
	if(argc!=3)
	{
		printf("Usage:mycp file1 file2\n");
		exit(1);//异常退出
	}
	
	//声明变量
	FILE* fp_r,*fp_w;
	char buf[1];
	//使用标准库函数尝试打开文件
	//以‘rt’文本形式打开file1
	if((fp_r = fopen(argv[1],"rt"))==NULL)
	{
		printf("Fail to open file:%s",argv[1]);
		exit(1);//异常退出
	} 
	//以‘w’文本形式打开file2（若文件存在则clear文件内容。若文件不存在则建立该文件）
	else if((fp_w = fopen(argv[2],"w"))==NULL){
		printf("Fail to open file:%s",argv[1]);
		exit(1);//异常退出
	}
	else{
		printf("Succecc to open file\n");
		while(!feof(fp_r))//当指针指向文件末尾时返回0
		{
			int i=0;
			//先读,每次读量为char[1]
			if((fread(buf,sizeof(char),1,fp_r))!=0)
			{
				//再写
				if(fwrite(buf,sizeof(buf),1,fp_w))
				{;}
				
			}	
		}
		printf("Tips:Read completed\n");
		exit(0);
	}

	printf("Tips:Copy completed\n");

	//退出程序之前关闭文件指针
	fclose(fp_r);
	fclose(fp_w);
	return 0;
}
